package category

import (
	"go-interview-forum/model/category"
)

func GetCategoryList() ([]category.CateList, error) {
	// 查一级分类
	firstCateList, err := category.GetCateListByLevel(1)
	if err != nil {
		return nil, err
	}

	// 二级分类
	secondCateList, err := category.GetCateListByLevel(2)
	if err != nil {
		return nil, err
	}

	var cateList []category.CateList

	secondListMap := make(map[int64][]category.Category)
	for _, v := range secondCateList {
		secondListMap[v.PId] = append(secondListMap[v.PId], v)
	}

	// 组装
	for _, v := range firstCateList {
		if _, ok := secondListMap[int64(v.ID)]; !ok {
			continue
		}
		cateList = append(cateList, category.CateList{
			Category: v,
			SubList:  secondListMap[int64(v.ID)],
		})
	}

	return cateList, nil
}

// 获取某个等级的分类列表
func GetAnyLevelCate(pid int, level int) ([]category.Category, error) {
	return category.GetCateList(pid, level)
}

// 根据名称搜索分类
func GetCateByCateName(name string) ([]category.Category, error) {
	return category.GetCateByCateName(name)
}

// 根据名称搜索分类--模糊查
func GetCateLikeCateName(name string) ([]category.Category, error) {
	return category.GetCateLikeCateName(name)
}
